package response

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Res struct {
	Code    int         `json:"code"`
	Success bool        `json:"success"`
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
}

func Success(ctx *gin.Context, data interface{}) {
	ctx.JSON(http.StatusOK, Res{
		Code:    0,
		Success: true,
		Msg:     "success",
		Data:    data,
	})
}

func SendErr(ctx *gin.Context, ec error) {
	if cusErr, ok := ec.(*MError); ok {
		ctx.JSON(http.StatusOK, Res{
			Code:    cusErr.Code,
			Success: false,
			Msg:     cusErr.Msg,
			Data:    nil,
		})
		return
	}

	ctx.JSON(http.StatusOK, Res{
		Code:    1,
		Success: false,
		Msg:     ec.Error(),
		Data:    nil,
	})
}
